﻿
$(document).ready(function () {
    $(".calendar-date").datepicker();
});

// First: Create config
ko.validation.configure({
	registerExtenders: true,
	messagesOnModified: true,
	insertMessages: true,
	parseInputAttributes: true,
	messageTemplate: null,
	decorateElement: true,
	errorClass: "error",
	errorMessageClass: "error-desc"
});

// Second: Create new model to apply to view
var viewModel = new function () {
    // About you
    this.Title = ko.observable().extend({ maxLength: 250 }).extend({ required: { message: "Tiêu đề tài liệu là bắt buộc"} });
    this.ShortDesc = ko.observable();
    this.LongDesc = ko.observable();
    this.Active = ko.observable(true);

    // Categories
    this.CategoryList = ko.observableArray(data.ListCategory);
    this.CategoryID = ko.observable($.cookie("member_categoryId") != null ? $.cookie("member_categoryId") : data.ListCategory[3].ID);
    this.SubCategoryOptionList = ko.observableArray(data.ListSubCategory);
    this.SubCategoryList = ko.computed(function () {
        var cate = this.CategoryID();
        return ko.utils.arrayFilter(this.SubCategoryOptionList(), function (option) {
            return option.ParentID == cate;
        });
    }, this);
    this.SubSelectedID = ko.computed(function () {
        var subCatCookie = $.cookie("member_subcategoryId") != null ? $.cookie("member_subcategoryId") : 0;
        var subCatID = this.SubCategoryList().length > 0? this.SubCategoryList()[0].ID : 0;
        if (subCatCookie != 0)
            return subCatCookie;
        else
            return subCatID;
    }, this);

    // State
    this.StateList = ko.observableArray(data.ListState);
    this.StateID = ko.observable($.cookie("member_stateId") != null ? $.cookie("member_stateId") : "HAN");
    this.DistrictOptionList = ko.observableArray(data.ListDistrict);
    this.DistrictList = ko.computed(function () {
        var state = this.StateID();
        return ko.utils.arrayFilter(this.DistrictOptionList(), function (option) {
            return option.ParentID.toLowerCase() == state.toLowerCase();
        });
    }, this);

    this.DistrictSelectedID = ko.computed(function () {
        var distCookie = $.cookie("member_districtId") != null ? $.cookie("member_districtId") : 0;
        var distID = this.DistrictList().length > 0? this.DistrictList()[0].ID : 0;
        if (distCookie != 0)
            return distCookie;
        else
            return distID;
    }, this);

    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            // set cookie
            $.cookie("member_categoryId", $('#CategoryID').val(), { path: '/', expires: 365 });
            $.cookie("member_subcategoryId", $('#SubCategoryID').val(), { path: '/', expires: 365 });
            $.cookie("member_stateId", $('#StateID').val(), { path: '/', expires: 365 });
            $.cookie("member_districtId", $('#DistrictID').val(), { path: '/', expires: 365 });

            // Submit form
            $('form').submit();
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
};

viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));